package com.jstyle.beatecg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.jstyle.beatecg.R;
import com.jstyle.beatecg.model.MyBluetoothDevice;

import java.util.ArrayList;
import java.util.List;

public class ScanDeviceAdapter extends BaseAdapter {
	private List<MyBluetoothDevice> devices;

	private LayoutInflater mInflator;
	public ScanDeviceAdapter(Context context) {
		mInflator= LayoutInflater.from(context);
		devices=new ArrayList<>();
	}



	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return devices.size();
	}

	@Override
	public Object getItem(int position) {
		if(devices==null){
			return 0;
		}else{
			return	devices.get(position);
		}
	}

	public List<MyBluetoothDevice> devices(){
		return devices;
	}


	/*@Override
	public BluetoothDevice getItem(int position) {
		// TODO Auto-generated method stub
		if(devices==null){
			return	devices.get(position);
		}else{
			return 0;
		}
	}*/

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		// General ListView optimization code.
		if (view == null) {
			view = mInflator.inflate(R.layout.listitem_device, null);
			viewHolder = new ViewHolder();
			viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
			viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
			viewHolder.deviceRssi=(TextView)view.findViewById(R.id.device_rssi);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		final String deviceName = devices.get(position).getNames();
		if (deviceName != null && deviceName.length() > 0)
			viewHolder.deviceName.setText(deviceName);

		else
			viewHolder.deviceName.setText("Unkown Device");
		viewHolder.deviceAddress.setText(devices.get(position).getDeviceAddress());
		viewHolder.deviceRssi.setText("Rssi:"+devices.get(position).getRssis());
		final int rssiPercent = Math.abs(devices.get(position).getRssis());
		//viewHolder.iv_rssi.setImageLevel(rssiPercent);
		return view;
	}
	public void clear() {
		devices.clear();
		notifyDataSetChanged();
	}

	public void addDevice(MyBluetoothDevice device) {
		// TODO Auto-generated method stub
		boolean value=false;
		for (MyBluetoothDevice str : devices) {
			if(str.getNames().contains(device.getNames())){
				value=true;
				return;
			}else{
				value=false;
			}
		}
		if(!value){
			devices.add(device);
			//排序下
			if(devices.size()>1){
				devices=Soretlist(devices);
			}
			notifyDataSetChanged();
		}
	}



	private List<MyBluetoothDevice> Soretlist(List<MyBluetoothDevice> devices){
		//使用冒泡排序算法
		for(int i=0;i<devices.size()-1;i++){
			for(int j=0;j<devices.size()-1-i;j++){
				if(devices.get(j).getRssis()<devices.get(j+1).getRssis()){
					MyBluetoothDevice devive=devices.get(j);
					devices.set(j, devices.get(j+1));
					devices.set(j+1, devive);
				}
			}
		}
		return  devices;
	}





	static class ViewHolder {
		TextView deviceName;
		TextView deviceAddress;
		TextView deviceRssi;
	}
}
